package org.hxzon.cyclone.pfgen;

import java.util.ArrayList;
import java.util.List;

import org.hxzon.cyclone.Particle;

//粒子和力发生器的注册中心
public class ParticleForceRegistry {

    protected static class ParticleForceRegistration {
        Particle particle;
        ParticleForceGenerator fg;
    };

    private List<ParticleForceRegistration> registrations = new ArrayList<ParticleForceRegistration>();

    public void add(Particle particle, ParticleForceGenerator fg) {
        ParticleForceRegistration registration = new ParticleForceRegistration();
        registration.particle = particle;
        registration.fg = fg;
        //registrations.push_back(registration);
        registrations.add(registration);
    }

    public void remove(Particle particle, ParticleForceGenerator fg) {
        ParticleForceRegistration t = null;
        for (ParticleForceRegistration r : registrations) {
            if (r.particle == particle && r.fg == fg) {
                t = r;
                break;
            }
        }
        registrations.remove(t);
    }

    public void clear() {
        registrations.clear();
    }

    public void updateForces(double duration) {
        for (ParticleForceRegistration pfr : registrations) {
            pfr.fg.updateForce(pfr.particle, duration);
        }
    }

}
